//indent-list.js
//获取应用实例  
var app = getApp()
Page({
    data: {
        /**
         * 页面配置
         */
        siteInfo: app.siteInfo,
        winWidth: 0,
        winHeight: 0,
    },
    onLoad: function () {
        var that = this;
        var carIds = wx.getStorageSync('toOrderCardIds');
        var param = {};
        param.cart_id = carIds;
        //获取商品列表
        app.util.request({
            'url': 'order/getCardGoodList',
            'data': param,
            'method': 'POST',
            'showLoading': false,
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: function (res) {
                var data = res.data.data;
                that.setData({
                    'data': data,
                });
            }
        });
    },
    onShow: function () {
        var that = this;
        //获取用户地址 更新
        app.util.request({
            'url': 'user/get_user_address',
            'showLoading': false,
            success: function (res) {
                var data = res.data.data;
                that.setData({
                    ['data.user.address']: data,
                });
            }
        });
    },
    //填写收货地址
    setAddress: function () {
        wx.navigateTo({
            url: '/wxz_superstore/pages/setMessage/addUserAddress'
        })
    },
    //支付
    gotoPay: function () {
        var that = this;
        if (!that.data.data.user.address.id) {
            wx.showModal({
                content: '请先填写收货信息',
                success: function (res) {
                    if (res.confirm) {
                        that.setAddress();
                    }
                }
            })
            return;
        }

        var carIds = wx.getStorageSync('toOrderCardIds');
        wx.removeStorageSync('toOrderCardIds');
        var param = {};
        param.address_id = that.data.data.user.address.id;
        param.cart_id = carIds;

        app.util.getUserInfo(() => {
            //请求支付接口
            app.util.request({
                'url': 'entry/wxapp/pay', //调用wxapp.php中的doPagePay方法获取支付参数
                'method': 'POST',
                data: param,
                'cachetime': '0',
                success(res) {
                    if (res.data && res.data.data && !res.data.errno) {
                        //发起支付
                        wx.requestPayment({
                            'timeStamp': res.data.data.timeStamp,
                            'nonceStr': res.data.data.nonceStr,
                            'package': res.data.data.package,
                            'signType': 'MD5',
                            'paySign': res.data.data.paySign,
                            'success': function (res) {
                                //执行支付成功提示
                                wx.showToast({
                                    title: '支付成功',
                                    icon: 'success',
                                    success: function () {
                                        wx.redirectTo({
                                            url: '/wxz_superstore/pages/order-list/order-list?order_status=all',
                                        });
                                    }
                                });
                            },
                            'fail': function (res) {
                                if (res.errMsg == 'requestPayment:fail cancel') {
                                    wx.redirectTo({
                                        url: '/wxz_superstore/pages/order-list/order-list?order_status=all',
                                    });
                                }
                            }
                        })
                    }
                },
                fail(res) {
                    wx.showModal({
                        title: '系统提示',
                        content: res.data.message ? res.data.message : '错误',
                        showCancel: false,
                        success: function (res) {
                            if (res.confirm) {
                                // backApp();
                            }
                        }
                    })
                }
            })
        });

    }

})  